import cn.hutool.core.date.DateUtil;
import org.apache.log4j.Logger;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 定时任务线程类
 */
public class ScheduleTask implements Runnable{
    private static Logger LOG=Logger.getLogger(ScheduleTask.class.getClass());

    protected Integer id;//任务唯一识别码

    public Integer getId(){
        return id;
    }
    public ScheduleTask(Integer id){
        this.id=id;
    }
    @Override
    public void run() {
        LOG. info(id+"开始执行，时间:"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        System.out.println(id + "开始执行，时间:" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));


    }

    private static Date addSenconds(int second){
        LocalDateTime localTime = LocalDateTime.now().plusSeconds(second);
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localTime.atZone(zone).toInstant();
        Date date = Date.from(instant);
        return date;
    }
    public static void main(String[] args) throws InterruptedException {
        System.out.println("当前时间:"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        System.out.println("开始定时任务");
        Date addSenconds = addSenconds(10);
        //任务一
        //ScheduleUtil.start(new ScheduleTask(1),addSenconds);
        //任务二
        Date addSenconds2 = addSenconds(20);
        ScheduleUtil.start(new ScheduleTask(2),addSenconds2);
        //任务三
        Date addSenconds3 = addSenconds(30);
        ScheduleUtil.start(new ScheduleTask(3),addSenconds3);
        //取消任务二
        ScheduleUtil.cancel(new ScheduleTask(2));
        //修改任务三
        Date addSenconds31 = addSenconds(60);
        ScheduleUtil.reset(new ScheduleTask(3),addSenconds31);

    }
}
